#include "BeanManager.h"
#include "IBean.h"
#include"IXML.h"
#include "IPlugin.h"

extern void initXMLBean();

void BeanManager::load(const char*filename)
{
    initXMLBean();
    BeanFactoryManager::getInstance().init();
    IXML *xml = Bean<IXML>("xml").pointer();
    if (xml) {
        if (xml->initFrom(filename))
        {
            INode * node = xml->firstNode("bean");
            while (node)
            {
                std::string name = node->getStr("name");
                std::string implemation = node->getStr("implemation");
                std::string interface = node->getStr("interface");
                IPlugin *bean = (IPlugin*)IPlugin::loadPlugin(implemation.c_str());
                BeanFactoryManager::getInstance().getFactory(interface.c_str())->addIBean(name.c_str(),bean);
                bean->onLoad(node);
                //printf("create bean name:%s implemation:%s interface:%s\n",name.c_str(),implemation.c_str(),interface.c_str());
                beans.push_back(bean);
                node = node->nextNode("bean");
            }
        }
    }
   
}

void BeanManager::unload()
{
  //  printf("BeanManager::unload %u\n",beans.size());
    for (auto it = beans.begin(); it != beans.end();++it) {
        if (*it) {
            (*it)->onUnload();
        }
    }
}
void BeanManager::start()
{
  //  printf("BeanManager::start %u\n",beans.size());
    for (auto it = beans.begin(); it != beans.end();++it) {
        if (*it) {
            (*it)->onStart();
        }
    }
}